var SERVER_PORT = "http://120.77.223.88:8080/red/";
var userMessage = new Vue({
    el: '#app-userMessage',
    data: {
        //登录账户
        isLogin: false,
        phone: "",
        nickname: "",

        name:"",
        pwd_old:"",
        pwd_new:"",
        pwd_new2:"",
        gender:"",

         //得到所有目录
         primaryCategory: [],



    },
    methods: {
        //退出登录
        quitLogin: function () {
            this.isLogin = false;
            this.phone = "";
            this.nickname = "";
            window.localStorage.setItem("isLogin", false);
            window.location.reload(true);
        },

        getUserInfo: function () {
            this.$http.post(SERVER_PORT + "Login", {
                type:1,
                phone: this.phone
            }, {emulateJSON: true})
                .then((res) => {
                    if (res.data.status == 1) {
                        var user = res.data.data[0];
                        this.name = this.nickname;
                        this.gender = user.gender;
                    } else {
                        console.log("获取个人数据失败");
                        alert("获取个人数据失败");
                    }
                }, (res) => {
                    alert("获取个人数据失败: " + res);
                });
        },

        submitInfo: function () {
            if(this.password !== window.localStorage.getItem("password")){
                alert("原始密码错误");
            } else {
                if(this.pwd_new === this.pwd_new2){
                    this.$http.post(SERVER_PORT + "modifyUser", {
                        phone:this.phone,
                        password:this.password,
                        nickname:this.nickname,
                        gender: this.gender
                    }, {emulateJSON: true})
                        .then((res) => {
                            if (res.data.status == 1) {
                                window.localStorage.setItem("nickname", this.name);
                                window.localStorage.setItem("password", this.pwd_new);
                            } else {
                                console.log("修改失败");
                                alert("修改失败");
                            }
                        }, (res) => {
                            alert("修改失败: " + res);
                        });
                } else {
                    alert("新密码前后不匹配");
                }
            }
        },

        onPrimCateClick: function (index) {
            console.log(index);
            window.localStorage.setItem("primIndex_goodCart", index);
            window.location.href = "searchResult.html"
        },

        getAllCategory: function () {
            this.$http.get(SERVER_PORT + "requestCategory").then(
                response => {
                    //console.log(response.data.data);
                    var originalChaoticInfo = response.data.data;

                    for (var index in originalChaoticInfo) {
                        var primaryCategoryName = originalChaoticInfo[index].category1_Name;
                        var primaryCategoryID = originalChaoticInfo[index].category1_ID;

                        var primaryCategoryItem = {
                            name: primaryCategoryName,
                            ID: primaryCategoryID
                        }

                        // var secondCategory = [];

                        // for (var sub_index in originalChaoticInfo[index].category2) {
                        //     var secondCategoryName =
                        //         originalChaoticInfo[index].category2[sub_index].name;
                        //     var category2_ID =
                        //         originalChaoticInfo[index].category2[sub_index].category2_ID;
                        //     secondCategory.push({
                        //         name: secondCategoryName,
                        //         ID: category2_ID
                        //     });
                        // }

                        // this.categoryTree.push({
                        //     primaryCategory: primaryCategory,
                        //     secondCategory: secondCategory
                        // });
                        this.primaryCategory.push(primaryCategoryItem);
                    }

                },
                response => {
                    // 响应错误回调
                    alert("请求失败");
                }
            );
        },

    },
    created: function () {
        this.getAllCategory();

        
        if(window.localStorage.getItem("isLogin") === undefined || window.localStorage.getItem("isLogin") === "false"){
            alert("请先登录");
            window.location.href="login.html";
        }else{
            this.isLogin = true;
            this.nickname = window.localStorage.getItem("nickname");
            this.phone = window.localStorage.getItem('phone');
            this.getUserInfo();
        }
    }
});